import oss2
from minio import Minio

if __name__ == "__main__":
    # Initialize MinIO client
    minio_client = Minio(
        "120.26.223.235:9000",
        access_key="minioadmin",
        secret_key="minioadmin",
        secure=False,
    )
    # Initialize OSS client
    auth = oss2.Auth("LTAIRLPGtcaInhNm", "1T2VecQIKi4RNmqHGBU3zx6v3j22e1")
    bucket = oss2.Bucket(auth, "http://oss-cn-hangzhou.aliyuncs.com", "tedek")
    # List all objects in the MinIO bucket
    minio_objects = minio_client.list_objects("tedek", prefix="weixin/", recursive=True)
    # Iterate through each object in the MinIO bucket
    for minio_object in minio_objects:
        # Get the object name
        object_name = minio_object.object_name
        # Get the object size
        object_size = minio_object.size
        # Print the object name and size
        print(f"Object Name: {object_name}, Size: {object_size} bytes")
        try:
            # Get object data from MinIO
            minio_obj = minio_client.get_object("tedek", object_name)
            # Read the entire content into memory
            object_data = minio_obj.read()
            # Upload the object to OSS
            bucket.put_object(object_name, object_data)
            print(f"Uploaded {object_name} to OSS.")
        finally:
            # Ensure the MinIO object stream is closed
            if 'minio_obj' in locals() and minio_obj:
                minio_obj.close()